<?php
class AppAdminController extends BaseController
{
	public $layout='//layouts/layout';
	public $mgrlang;
	public $connection;
    public $data = array();
    public $messagedata = array();
    public $user;
    public $activemenu;

    public function init()
    {
        $this->connection = Yii::app()->db;

        /*管理后台内容的语言*/
        if(isset($_GET['mgrlang']) && $_GET['mgrlang'] != "")
        {
            // 设置COOKIE，
            Yii::app()->request->cookies['mgrlang'] = new CHttpCookie('mgrlang', $_GET['mgrlang']);
            $this->mgrlang = $_GET['mgrlang'];
        }
        else if (isset(Yii::app()->request->cookies['mgrlang']) && Yii::app()->request->cookies['mgrlang']->value != "")
        {
            // 根据COOKIE中语言类型来设置语言
            $this->mgrlang = Yii::app()->request->cookies['mgrlang']->value;
        }else{
            Yii::app()->request->cookies['mgrlang'] = new CHttpCookie('mgrlang', 'zh_cn');
            $this->mgrlang = 'zh_cn';
        }

        $user = Yii::app ()->session ['user'];
        if(!empty($user)){
            $this->user = $user;
        }

        $this->initCommonData();

    }

    /**
     * 初始化后台所需要的数据
     */
    public function initCommonData(){

        $langs = $this->connection->createCommand("select * from xm_lang where mark !='rkcms'")->queryAll();
        $this->data['langs'] = $langs;

    }

	public function filters() {

		$current_uri = Yii::app()->request->requestUri;

        if(strpos($current_uri,'ajax')){
            $this->layout = "//layouts/ajax";
        }

		return array (
				array('application.filters.AuthorityFilter')
		);
	}

    function message($type,$message,$data=null){
        echo json_encode(Array('type'=>$type,'message'=>$message,'data'=>$data));
    }

    public function saveConfig(){
        $configs = $this->connection->createCommand("select * from xm_config where lang = 'system'")->queryAll();
        $cfg_string ='<?php ';
        $cfg_string .="\r\n";
        foreach($configs as $cfg){
            $cfg_string .='$APPCFG[\''.$cfg['syskey'].'\'] = \''.$cfg['content'].'\';';
            $cfg_string .="\r\n";
        }
        file_put_contents("config/config.php",$cfg_string);
    }
	
}